import {
  USER_LOGIN,
  LOAD_USER,
  USER_LOGOUT
} from '../constants/actionType'
import Immutable from 'immutable'

import User from '../models/user'

const initialState = Immutable.fromJS({
  user: new User()
})

export default (state = initialState, action) => {
  let newState = state
  switch (action.type) {
    case LOAD_USER:
    case USER_LOGIN:
      newState = newState.set('user', action.payload.user)
      return newState
    case USER_LOGOUT:
      console.log('=USER_LOGOUT==')
      User.delete()
      newState = newState.set('user', new User())
      return newState
    default :
      return state
  }
}
